using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace KFGame
{
    /// <summary>
    /// 框架UI组件，显视所有对象之前
    /// 
    /// </summary>
    [DisallowMultipleComponent]
    public class KFUI : MonoBehaviour
    {
        public KFMessage message;
        public KFFPS FPS;
        public Dictionary<string, KFMessage> messages = new Dictionary<string, KFMessage>();


        /// <summary>
        /// 关闭所有消息窗口
        /// </summary>
        public void closeAllMessage()
        {
            foreach (var item in messages.Values)
            {
                item.closeMessage();
                item.toDestroy();
            }
            messages.Clear();
        }

        /// <summary>
        ///  关闭并消毁 信息框
        /// </summary>
        /// <param name="name">信息框定义名字</param>
        /// 
        public void closeMessage(string name)
        {
            KFMessage message;
            messages.TryGetValue(name, out message);
            if (message == null)
                return;
            message.closeMessage();
            message.toDestroy();
            messages.Remove(name);
        }


        /// <summary>
        ///  显示 信息
        /// </summary>
        /// <param name="msg">信息内容</param>
        /// <param name="title">信息标题</param>
        /// <param name="delayed"> 时间控制自动关闭单位秒，默认是0，不会自动关闭。使用 closeMessage（） 手动关闭 </param>
        /// <param name="name">定义信息的名字，相同名字的信息会被复盖</param>
        /// 
        public void showMessage(string msg, string title = null, float delayed = 0, string name = "kfmessage")
        {
            closeMessage(name);

            GameObject msgobj = Instantiate(message.gameObject, transform) as GameObject;
            if (msgobj == null)
            {
                KF.Log("KFUI error Instantiate is null cant open message gameobject");
                return;
            }

            KFMessage kFMessage = msgobj.GetComponent<KFMessage>();
            if (kFMessage == null)
            {
                KF.Log("KFUI error Instantiate KFMessage is null cant open message component");
                return;
            }

            kFMessage.setMessage(name);
            kFMessage.showMessage(msg, title, delayed);
            //msgobj.SetActive(true);
            messages.Add(name, kFMessage);

        }

        /// <summary>
        ///  显示 信息
        /// </summary>
        /// <param name="msg">信息内容</param>
        /// <param name="title">信息标题</param>
        /// <param name="delayed"> 时间控制自动关闭单位秒，默认是0，不会自动关闭。使用 closeMessage（） 手动关闭 </param>
        /// <param name="name">定义信息的名字，相同名字的信息会被复盖</param>
        /// 
        public void showMessageToCenter(string msg, string title = null, float delayed = 0, string name = "kfmessage")
        {
            closeMessage(name);

            GameObject msgobj = Instantiate(message.gameObject, transform) as GameObject;
            if (msgobj == null)
            {
                KF.Log("KFUI error Instantiate is null cant open message gameobject");
                return;
            }

            KFMessage kFMessage = msgobj.GetComponent<KFMessage>();
            if (kFMessage == null)
            {
                KF.Log("KFUI error Instantiate KFMessage is null cant open message component");
                return;
            }


            kFMessage.pointlocation = LocationPoint.Center;
            kFMessage.setMessage(name);
            kFMessage.showMessage(msg, title, delayed);
            // msgobj.SetActive(true);
            messages.Add(name, kFMessage);
        }


        Dictionary<string, GameObject> views = new Dictionary<string, GameObject>();
        /// <summary>
        /// 加载入到UI视图组件
        /// </summary>
        public GameObject loadViewResources(string name)
        {
            var v = Resources.Load(name) as GameObject;
            if (v != null)
            {

                GameObject tempInstance = Instantiate(v, transform.parent);
                views.Add(name, tempInstance);
                return tempInstance;
            }
            return null;

        }


        /// <summary>
        /// 消毁资源
        /// </summary>
        /// <param name="name"></param>
        public void destroyView(string name)
        {
            if (views.ContainsKey(name))
            {
                if (views[name] != null)
                    Destroy(views[name]);
                views.Remove(name);
            }
        }

        /// <summary>
        /// 展示组件
        /// </summary>
        /// <param name="name"></param>
        public void showView(string name)
        {

            if (!views.ContainsKey(name))
            {
                var v = loadViewResources(name);
                if (v != null)
                    v.SetActive(true);
                return;
            }

            if (views[name] != null)
                views[name].SetActive(false);

        }

        /// <summary>
        /// 关闭展示组件
        /// </summary>
        /// <param name="name"></param>
        public void closeView(string name)
        {
            if (views.ContainsKey(name))
                if (views[name] != null)
                    views[name].SetActive(false);

        }

        private static KFUI ui;



        /// <summary>
        /// 接收事件
        /// </summary>
        /// <param name="e"></param>
        void OnKFevent(KFEvent e)
        {


            if (e.eventContains("KF_UIOPEN_"))
            {
                //重复事件，开关效果
                if (views.ContainsKey(e.valueString))
                {
                    closeView(e.valueString);
                    destroyView(e.valueString);
                    return;
                }
                showView(e.valueString);
                return;
            }

            if (e.eventContains("KF_UICLOSE_"))
            {
                closeView(e.valueString);
                return;
            }
        }


        void Awake()
        {
            ui = this;
        }
        void Start()
        {


        }

        // Update is called once per frame
        void Update()
        {

        }
    }
}